package com.dongdongshop.service.impl;

import com.dongdongshop.admin.model.TbContentCategory;
import com.dongdongshop.admin.model.TbContentCategoryExample;
import com.dongdongshop.admin.vo.ContentCategoryVO;
import com.dongdongshop.mapper.TbContentCategoryMapper;
import com.dongdongshop.service.CateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CateServiceImpl implements CateService {
    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;

    @Override
    public List<ContentCategoryVO> getAll() {
        List<TbContentCategory> tbContentCategories = tbContentCategoryMapper.selectByExample(null);
        return tbContentCategories.stream().map(i->{
            ContentCategoryVO vo = new ContentCategoryVO();
            BeanUtils.copyProperties(i, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public void add(ContentCategoryVO contentCategoryVO) {
        TbContentCategory tbContentCategory = new TbContentCategory();
        BeanUtils.copyProperties(contentCategoryVO, tbContentCategory);
        tbContentCategoryMapper.insertSelective(tbContentCategory);
    }

    @Override
    public List<ContentCategoryVO> getById(Long id) {
        TbContentCategoryExample e = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = e.createCriteria();
        criteria.andIdEqualTo(id);
        List<TbContentCategory> t = tbContentCategoryMapper.selectByExample(e);
        return t.stream().map(i->{
            ContentCategoryVO c = new ContentCategoryVO();
            BeanUtils.copyProperties(i,c);
            return c;
        }).collect(Collectors.toList());
    }

    @Override
    public void update(ContentCategoryVO contentCategoryVO) {
        TbContentCategory tbContentCategory = new TbContentCategory();
        BeanUtils.copyProperties(contentCategoryVO, tbContentCategory);
        tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
    }
}
